package com.arvato.common.util;

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

/**
 * spring下载工具类
 * @author ZHAN655
 *
 */
public class SpringDownloadUtil {
	/**
	 * Spring下载文件公用
	 * @param fileFullPath ：服务器上的文件路径
	 * @param fileName ：返给客户端的文件名称
	 * @return 
	 * @throws IOException 
	 */
	public static ResponseEntity<byte[]> download(String fileFullPath, String fileName) throws IOException {
		File file = new File(fileFullPath);
		HttpHeaders headers = new HttpHeaders();
		fileName = new String((fileName).getBytes("UTF-8"), "iso-8859-1");
		// 为了解决中文名称乱码问题
		headers.setContentDispositionFormData("attachment", fileName);
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.OK);
	}
}
